﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ResumeService;
using System.Web.Security;
using AvatarHelper;
using Avatar.MembershipService;
using Avatar.Model;
public partial class PersonEducation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PersonHelper.BasicInformationJobIntentionCheck();

        if (!IsPostBack)
        {
            ddl_Year2.Items.Add(new ListItem("年", "0"));
            for (int _i = 2011; _i > 1940; _i--)
            {
                ddl_Year1.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
                ddl_Year2.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
            }

            ddl_Month2.Items.Add(new ListItem("月", "0"));
            for (int _i = 1; _i < 13; _i++)
            {
                ddl_Month1.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
                ddl_Month2.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
            }

            using (var _adc = DBHelper.GetDataContext())
            {
                DropDownListHelper.Bind(ddl_Diploma, _adc.Diploma.ToList(), "name", "id");

                ddl_Disciplines1.DataSource = _adc.Disciplines.Where(d=>d.FatherID==0);
                ddl_Disciplines1.DataTextField = "Name";
                ddl_Disciplines1.DataValueField = "ID";
                ddl_Disciplines1.DataBind();

                ddl_Disciplines2.DataSource = _adc.Disciplines.Where(d=>d.FatherID==Convert.ToInt32(ddl_Disciplines1.SelectedValue));
                ddl_Disciplines2.DataTextField = "Name";
                ddl_Disciplines2.DataValueField = "ID";
                ddl_Disciplines2.DataBind();

                var _user = LoginManager.GetCurrentUser();
                if (_user != null)
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user.UserName).FirstOrDefault();
                    if (_resume != null)
                    {
                        if (_resume.Education.Count > 0)
                        {
                            ListView1.DataSource = _resume.Education;
                            ListView1.DataBind();
                        }
                    }
                }
            }
        }
    }
    protected void ddl_Disciplines1_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            ddl_Disciplines2.DataSource = _adc.Disciplines.Where(d => d.FatherID == Convert.ToInt32(ddl_Disciplines1.SelectedValue));
            ddl_Disciplines2.DataTextField = "Name";
            ddl_Disciplines2.DataValueField = "ID";
            ddl_Disciplines2.DataBind();
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        Button _btn = sender as Button;
        Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);
        using (var _adc = DBHelper.GetDataContext())
        {
            var _e = _adc.Education.Where(edu => edu.ID == _id).FirstOrDefault();
            if (_e != null)
            {
                txt_ID.Value = _e.ID.ToString();
                ddl_Year1.SelectedValue = _e.BeginDate.Year.ToString();
                ddl_Month1.SelectedValue = _e.BeginDate.Month.ToString();
                if (_e.OverDate != DateTime.MinValue)
                {
                    ddl_Year2.SelectedValue = _e.OverDate.Year.ToString();
                    ddl_Month2.SelectedValue = _e.OverDate.Month.ToString();
                }
                ddl_Disciplines1.SelectedValue = _e.Disciplines.FatherID.ToString();
                ddl_Disciplines2.DataSource = _adc.Disciplines.Where(d=>d.FatherID==Convert.ToInt32(ddl_Disciplines1.SelectedValue));
                ddl_Disciplines2.DataBind();
                ddl_Disciplines2.SelectedValue = _e.DisciplinesID.ToString();
                SchoolTextBox.Text = _e.School;
                ddl_Disciplines2.SelectedValue = _e.DisciplinesID.ToString();
                ddl_Diploma.SelectedValue = _e.DiplomaID.ToString();
                DescriptionTextBox.Text = _e.Description;
                StudyAbroad.Checked = (bool)_e.StudyAbroad;
                DescriptionTextBox.Text = _e.Description;
            }
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            Button _btn = sender as Button;
            Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);
            
            EducationManager.Delete(_id);

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_SaveNew_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect("./PersonResume.aspx");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void Save()
    {
        try
        {
            Guid _id = string.IsNullOrEmpty(txt_ID.Value) ? Guid.Empty : new Guid(txt_ID.Value);
            string _userName = Membership.GetUser().UserName;
            if (ddl_Year1.SelectedValue == "0" || ddl_Month1.SelectedValue == "0") throw new Exception("起始时间为空");
            DateTime _beginDate = Convert.ToDateTime(ddl_Year1.SelectedValue + "/" + ddl_Month1.SelectedValue);
            DateTime _overDate = DateTime.MinValue;
            if (ddl_Year2.SelectedValue != "0" && ddl_Month2.SelectedValue != "0")
            {
                _overDate = Convert.ToDateTime(ddl_Year2.SelectedValue + "/" + ddl_Month2.SelectedValue);
            }

            EducationManager.Save(_userName,
            _id,
            SchoolTextBox.Text,
            Convert.ToInt32(ddl_Diploma.SelectedValue),
            _beginDate,
            _overDate,
            DescriptionTextBox.Text,
            Convert.ToInt32(ddl_Disciplines2.SelectedValue),
            StudyAbroad.Checked
            );

        }
        catch (Exception _e)
        {
            throw new Exception(_e.Message);
        }
    }
}
